import 'package:flutter/material.dart';
import 'package:flutter_app10/configs/app_style_config.dart';
import 'package:flutter_app10/routers/app_routers.dart';
import 'package:flutter_app10/view/jk/jk_text.dart';


class HomeDrawerPage extends StatefulWidget {

  final Function(String) pageTurnhandle;

  const HomeDrawerPage({Key key, this.pageTurnhandle}) : super(key: key);
  @override
  _HomeDrawerPageState createState() => _HomeDrawerPageState();
}

class _HomeDrawerPageState extends State<HomeDrawerPage> {
  List dataSource = [
    {
      "icon": Icons.insert_drive_file,
      "title": "订单管理",
    },
    {
      "icon": Icons.settings_applications,
      "title": "费率设置",
      "tag": "专业费率重磅升级",
      "tagColor": Colors.orange
    },
    {"icon": Icons.sticky_note_2_outlined, "title": "我的优惠券"},
    {"icon": Icons.credit_card, "title": "我的点卡"},
    {"icon": Icons.lock, "title": "安全设置"},
    {
      "icon": Icons.settings,
      "title": "设置",
      "tag": "实时盯盘上线",
      "tagColor": Colors.blue,
      "warning":true,
    },
  ];

  List<String> _pageRouters = [
    AppRouters.orderManagePage,
    AppRouters.rateSetPage,
    AppRouters.myCouponPage,
    AppRouters.myCsolPage,
    AppRouters.safetySetPage,
    AppRouters.settingPage,
  ];

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: AppStyleConfig.themColor,
      body: Container(
        // height: MediaQuery.of(context).size.height,
        child: Column(
          children: [
            Expanded(
              child: Padding(
                padding: EdgeInsets.symmetric(horizontal: 16.0),
                child: ListView.builder(
                  itemBuilder: (context, index) {
                    if (index == 0) {
                      return headerWidget();
                    }
                    return cellWidget(data: dataSource[index - 1],index: index-1);
                  },
                  itemCount: dataSource.length + 1,
                ),
              ),
            ),
            Container(
              decoration: BoxDecoration(
                border: Border(
                  top: BorderSide(width: 0.2, color: Colors.white54),
                ),
              ),
              height: 44,
              child: Row(
                children: [
                  bottomButtonWidget(
                      icon: Icons.help,
                      title: "帮助中心",
                      handle: () {
                        print("跳转帮助中心");
                      }),
                  Container(
                    width: 0.2,
                    height: 44,
                    color: Colors.white54,
                  ),
                  bottomButtonWidget(
                      icon: Icons.share,
                      title: "分享应用",
                      handle: () {
                        print("分享应用");
                      }),
                ],
              ),
            ),
          ],
        ),
      ),
    );
  }

  Widget headerWidget() {
    return GestureDetector(
      onTap: (){
        Navigator.pop(context);
        widget.pageTurnhandle(AppRouters.loginPage);
      },
      child: Container(
        height: 160,
        padding: EdgeInsets.symmetric(vertical: 16.0),
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          mainAxisAlignment: MainAxisAlignment.end,
          children: [
            Row(
              mainAxisAlignment: MainAxisAlignment.spaceBetween,
              children: [
              Column(
                children: [
                  JKText(
                    data: "点击登录",
                    color: Colors.white,
                    weight: FontWeight.bold,
                    fontSize: 24,
                  ),
                  JKText(
                    data: "欢迎来到火币",
                    color: Colors.white,
                    fontSize: 14,
                  ),
                ],
              ),
              Icon(Icons.chevron_right,color: Colors.white70,size: 16,),
            ],),
            Container(
              margin: EdgeInsets.only(top: 10),
              width: MediaQuery.of(context).size.width,
              height: 36,
              child: Container(
                padding: EdgeInsets.symmetric(horizontal: 8.0),
                decoration: BoxDecoration(
                  color: Colors.black26,
                  borderRadius: BorderRadius.all(
                    Radius.circular(5),
                  ),
                ),
                child: Row(
                  mainAxisAlignment: MainAxisAlignment.spaceBetween,
                  children: [
                    Row(
                      children: [
                        Icon(
                          Icons.supervised_user_circle_rounded,
                          color: Colors.orangeAccent,
                        ),
                        SizedBox(
                          width: 8,
                        ),
                        JKText(
                          data: "了解VIP等级及权益",
                          color: Colors.orangeAccent,
                        ),
                      ],
                    ),
                    Icon(
                      Icons.arrow_forward_ios_rounded,
                      color: Colors.orangeAccent,
                      size: 16,
                    ),
                  ],
                ),
              ),
            ),
          ],
        ),
      ),
    );
  }

  Widget cellWidget({Map data,int index}) {
    return GestureDetector(
      behavior: HitTestBehavior.opaque,
      onTap: (){
        Navigator.pop(context);
        widget.pageTurnhandle(_pageRouters[index]);
      },
      child: Container(
        padding: EdgeInsets.symmetric(vertical: 16.0),
        child: Row(
          mainAxisAlignment: MainAxisAlignment.spaceBetween,
          children: [
           Row(children: [
             Icon(
               data["icon"],
               color: Colors.grey,
               size: 16,
             ),
             SizedBox(
               width: 8.0,
             ),
             JKText(
               data: data["title"],
               fontSize: 16,
               color: Colors.white,
               weight: FontWeight.w400,
             ),
             SizedBox(width: 4,),
             Visibility(
               visible: data["tag"] != null,
               child: Container(
                 padding: EdgeInsets.symmetric(horizontal: 4),
                 decoration: BoxDecoration(
                   borderRadius: BorderRadius.circular(2),
                   color: Colors.black26,
                 ),
                 child: JKText(
                   data: data["tag"],
                   color: data["tagColor"],
                   fontSize: 11,
                 ),
               ),
             ),
           ],),
            Visibility(
              visible: data["warning"] == true,
              child: Icon(Icons.privacy_tip_outlined,size: 16,color: Colors.red,),),
          ],
        ),
      ),
    );
  }

  Widget bottomButtonWidget({IconData icon, String title, Function handle}) {
    return Expanded(
      flex: 1,
      child: GestureDetector(
        onTap: handle,
        child: Container(
          padding: EdgeInsets.symmetric(vertical: 10.0),
          child: Row(
            mainAxisAlignment: MainAxisAlignment.center,
            children: [
              Icon(
                icon,
                color: Colors.white,
              ),
              SizedBox(
                width: 4,
              ),
              JKText(
                data: title,
                color: Colors.white,
                fontSize: 14,
              ),
            ],
          ),
        ),
      ),
    );
  }
}
